home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / bildschirmschoner / blitzblank_2.60 / developer / modulesources / bb.worms2.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  8KB  |  355 lines

  1. /*
  2.  * Module: BB.Worms2.c
  3.  *
  4.  * Aktion: Bunte Würmer, die über den Bildschirm kriechen.
  5.  *
  6.  * Version:
  7.  *   1.1  (28.01.95) Portierung nach BlitzBlank
  8.  *   1.0             Originalimplementation für SuperDark
  9.  *
  10.  * Autor:
  11.  *   Dirk Farin
  12.  *   Kapellenweg 15
  13.  *   72070 Tübingen
  14.  *   Germany
  15.  *
  16.  *   EMail: farindk@trick.informatik.uni-stuttgart.de
  17.  *
  18.  * Copyright:
  19.  *   Dieses Programm ist Freeware, es darf nur
  20.  *   unentgeltlich und unverändert kopiert werden.
  21.  */
  22.  
  23. #define __USE_SYSBASE 1
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include <dos.h>
  30. #include <dos/dos.h>
  31. #include <exec/memory.h>
  32. #include <intuition/intuitionbase.h>
  33. #include <intuition/screens.h>
  34. #include <intuition/intuition.h>
  35. #include <proto/exec.h>
  36. #include <proto/intuition.h>
  37. #include <proto/graphics.h>
  38. #include <blitzblank_pragmas.h>
  39. #include <BlitzBlank.h>
  40.  
  41. #include <time.h>
  42. #include <proto/dos.h>
  43.  
  44. struct Library *BlitzBlankBase;
  45. char *VersionString="$VER: BB.Worms2 1.1 (28.01.95)";
  46.  
  47. #define TXTBASE 470
  48.  
  49. #define TXT_INFO      0
  50. #define TXT_NWORMS    1
  51. #define TXT_LENGTH    2
  52. #define TXT_CRAZYNESS 3
  53. #define TXT_SPEED     4
  54.  
  55. char *text[]={"\33c\33uWorms2\33n\n\nVersion 1.1\n\nCopyright 1995\nby\nDirk Farin",
  56.               "_number of worms",
  57.               "worm _length",
  58.               "c_razyness",
  59.               "sp_eed"
  60.              };
  61.  
  62. struct BB_Object object[]={ {&object[1],BB_VGroup,0,0,0,NULL,NULL},
  63.                             {&object[2],BB_Slider,1,50,20,NULL,NULL},
  64.                             {&object[3],BB_Slider,10,200,50,NULL,NULL},
  65.                             {&object[4],BB_Slider,3,30,20,NULL,NULL},
  66.                             {&object[5],BB_Slider,1,6,4,NULL,NULL},
  67.                             {NULL      ,BB_VGroup_End,0,0,0,NULL,NULL} };
  68.  
  69. struct BB_Message message;
  70.  
  71. struct BB_Screeninfo *screeninfo;
  72. struct RastPort *rp;
  73. struct ViewPort *vp;
  74.  
  75.  
  76. long   scr_width;
  77. long   scr_height;
  78. USHORT scr_colors;
  79. int    scr_depth;
  80.  
  81.  
  82. static ULONG  nWorms=20;
  83. static ULONG  MaxWormLength=50;
  84. static ULONG  MaxTurn=20;
  85. static ULONG  Speed=4;
  86. static double forward_step =1.0;
  87.  
  88.  
  89.  
  90. struct Worm
  91. {
  92.   short  head;
  93.   short  angle;
  94.  
  95.   double cx,cy; // current head-position
  96.  
  97.   short  *x,*y;
  98. };
  99.  
  100. static struct Worm* worms = NULL;
  101.  
  102. static double sintab[90];
  103.  
  104. void InitSin(void)
  105. {
  106.     double w=0;
  107.     int i;
  108.  
  109.     for (i=0;i<90;i++)
  110.     {
  111.         w+=PI/180;
  112.         sintab[i]=sin(w);
  113.     }
  114. }
  115.  
  116. double GetSin(short angle)
  117. {
  118.     if (angle< 90) return  sintab[angle];
  119.     if (angle<180) return  sintab[179-angle];
  120.     if (angle<270) return -sintab[angle-180];
  121.                    return -sintab[359-angle];
  122. }
  123.  
  124. double GetCos(short angle)
  125. {
  126.     angle+=90;
  127.     if (angle>=360) angle-=360;
  128.  
  129.     return GetSin(angle);
  130. }
  131.  
  132. void StepWorms(void)
  133. {
  134.     struct Worm* worm;
  135.     int          n;
  136.     int          last;
  137.     double       newx,newy;
  138.  
  139.  
  140.     for ( n=0 ; n<nWorms ; n++ )
  141.     {
  142.         worm=&worms[n];
  143.  
  144.         last = worm->head+1;
  145.         if (last==MaxWormLength) last=0;
  146.  
  147.  
  148.         /* --- remove last point --- */
  149.  
  150.         SetAPen(rp,0);
  151.         WritePixel(rp,worm->x[last],worm->y[last]);
  152.  
  153.  
  154.         /* --- add a new point at the head --- */
  155.  
  156.         /* move head */
  157.  
  158.         newx = worm->cx + GetCos(worm->angle)*forward_step;
  159.         newy = worm->cy + GetSin(worm->angle)*forward_step;
  160.  
  161.         worm->angle += (rand()%(2*MaxTurn))-MaxTurn;
  162.  
  163.  
  164.         /* check for overflow */
  165.  
  166.         if      (newx <  0         ) newx += scr_width;
  167.         else if (newx >= scr_width ) newx -= scr_width;
  168.  
  169.         if      (newy <  0         ) newy += scr_height;
  170.         else if (newy >= scr_height) newy -= scr_height;
  171.  
  172.         if      (worm->angle >= 360) worm->angle-=360;
  173.         else if (worm->angle <  0  ) worm->angle+=360;
  174.  
  175.  
  176.         /* store values */
  177.  
  178.         worm->cx = newx;
  179.         worm->cy = newy;
  180.         worm->x[last] = newx;
  181.         worm->y[last] = newy;
  182.  
  183.         worm->head=last;
  184.  
  185.  
  186.         /* draw pixel */
  187.  
  188.         SetAPen(rp,(n%(scr_colors-1))+1);
  189.         WritePixel(rp,(int)newx,(int)newy);
  190.     }
  191. }
  192.  
  193. static char colors[32][3] =
  194. {
  195.      0, 0, 0,   15,15,15,   15, 0, 0,    0,15, 0,
  196.      0, 0,15,   15,15, 0,    0,15,15,   15, 0,15,
  197.     15, 8, 0,    8,15, 0,    0, 8,15,    0,15, 8,
  198.      8, 0,15,   15, 0, 8,    8, 8, 8,    8, 8,15,
  199.      8,15, 8,   15, 8, 8,    8, 0, 0,    0, 8, 0,
  200.      0, 0, 8,    8, 8, 0,    8, 0, 8,    0, 8, 8,
  201.      8, 4, 0,    4, 8, 0,    0, 4, 8,    0, 8, 4,
  202.      4, 0, 8,    8, 0, 4,    4, 8, 4,    8, 4, 4
  203. };
  204.  
  205. static int cnt=0;
  206.  
  207. void blank(void)
  208. {
  209.     int i;
  210.  
  211.     InitSin();
  212.     worms = NULL;
  213.  
  214.  
  215.     nWorms        = object[1].set;
  216.     MaxWormLength = object[2].set;
  217.     MaxTurn       = object[3].set;
  218.     Speed         = object[4].set;
  219.  
  220.     /* ask screen size */
  221.  
  222.     rp=&screeninfo->bbscreen->RastPort;
  223.     vp=&screeninfo->bbscreen->ViewPort;
  224.  
  225.     scr_width  = screeninfo->bbscreen->Width;
  226.     scr_height = screeninfo->bbscreen->Height;
  227.     scr_depth  = screeninfo->bbscreen->BitMap.Depth;
  228.     scr_colors = 1<<scr_depth;
  229.  
  230.  
  231.  
  232.     /* initialize worms */
  233.  
  234.     worms = calloc( sizeof(struct Worm) , nWorms );
  235.     if (!worms) goto cleanexit;
  236.  
  237.  
  238.     srand(time(NULL));
  239.  
  240.     for (i=0;i<nWorms;i++)
  241.     {
  242.         worms[i].x = calloc( sizeof(short) , MaxWormLength );
  243.         worms[i].y = calloc( sizeof(short) , MaxWormLength );
  244.  
  245.         if ( ( ! worms[i].x ) || ( ! worms[i].y ) ) goto cleanexit;
  246.  
  247.         worms[i].head = 0;
  248.  
  249.         worms[i].cx = worms[i].x[0]  =  rand()%scr_width;
  250.         worms[i].cy = worms[i].y[0]  =  rand()%scr_height;
  251.         worms[i].angle               =  rand()%360;
  252.     }
  253.  
  254.  
  255.     /* set colors */
  256.  
  257.     for (i=0;i<scr_colors;i++)
  258.       SetRGB4(vp,i,colors[i][0],colors[i][1],colors[i][2]);
  259.  
  260.  
  261.     /* move worms till end */
  262.  
  263.     ScreenToFront (screeninfo->bbscreen);
  264.  
  265.     if (!CheckSignal (SIGBREAKF_CTRL_C))
  266.     {
  267.       BBL_ModuleRunning ();
  268.  
  269.       do
  270.       {
  271.         switch (Speed)
  272.         {
  273.             case 2: StepWorms();
  274.                     break;
  275.  
  276.             case 3: StepWorms();
  277.                     cnt++;
  278.                     if (cnt&1) StepWorms();
  279.                     break;
  280.  
  281.             case 4: StepWorms();
  282.                     StepWorms();
  283.                     break;
  284.  
  285.             case 5: StepWorms();
  286.                     StepWorms();
  287.                     break;
  288.  
  289.             case 6: StepWorms();
  290.                     StepWorms();
  291.                     break;
  292.         }
  293.  
  294.         WaitTOF();
  295.       } while (!CheckSignal (SIGBREAKF_CTRL_C));
  296.     }
  297.  
  298. cleanexit:
  299.  
  300.     if (worms)
  301.     {
  302.         int i;
  303.         for (i=0;i<nWorms;i++)
  304.         {
  305.             if (worms[i].x) free(worms[i].x);
  306.             if (worms[i].y) free(worms[i].y);
  307.         }
  308.  
  309.         free(worms);
  310.     }
  311. }
  312.  
  313. void main(int argc,char **argv)
  314. {
  315.   if (!(BlitzBlankBase=OpenLibrary ("blitzblank.library",BLITZBLANKLIB_VER)))
  316.     exit (0);
  317.  
  318.   message.flags=BBF_Screenmode|BBF_Colors;
  319.   message.first=&object[0];
  320.  
  321.   StrToLong (argv[3],(long *) &screeninfo);
  322.   screeninfo->mindepth=0;
  323.   screeninfo->maxdepth=5;
  324.  
  325.   if (strcmp (argv[1],"BLANK")==0)
  326.   {
  327.     BBL_SendMessage (&message,argv[2]);
  328.     if (screeninfo->bbscreen)
  329.       blank ();
  330.     BBL_BlankDone ();
  331.   }
  332.   else
  333.   {
  334.     message.infotext=BBL_GetString (TXTBASE+TXT_INFO  ,text[TXT_INFO]);
  335.     object[1].label =BBL_GetString (TXTBASE+TXT_NWORMS,text[TXT_NWORMS]);
  336.     object[2].label =BBL_GetString (TXTBASE+TXT_LENGTH,text[TXT_LENGTH]);
  337.     object[3].label =BBL_GetString (TXTBASE+TXT_CRAZYNESS,text[TXT_CRAZYNESS]);
  338.     object[4].label =BBL_GetString (TXTBASE+TXT_SPEED ,text[TXT_SPEED]);
  339.  
  340.     if (strcmp (argv[1],"CONFIG")==0)
  341.     {
  342.       BBL_SendMessage (&message,argv[2]);
  343.     }
  344.     else
  345.     {
  346.       message.first=NULL;
  347.       BBL_SendMessage (&message,argv[2]);
  348.     }
  349.   }
  350.   CloseLibrary (BlitzBlankBase);
  351.   exit (0);
  352. }
  353.  
  354.  
  355.